unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

const
  N = 10;
  R = 15;
  L = 5;

type
  frfr = record
    num, x, y: integer;
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  mass: array [0..N-1] of integer;
  arr: array [0..(N*(N+1) div 2)-1] of frfr;
  step, sum: integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
  randomize;
  Canvas.Pen.Color:= clWhite;
  Canvas.Brush.Color:= clWhite;
  Canvas.Rectangle(Form1.ClientRect);
  for i := 0 to N - 1 do
  begin
    mass[i]:= random(10000000000);
    arr[i].num:= mass[i];
    arr[i].y:= L*(i+1)+i*2*R+R;
    arr[i].x:= L + R;
    Canvas.Brush.Color:= arr[i].num;
    Canvas.Pen.Color:= arr[i].num;
    Canvas.Ellipse(arr[i].x-R, arr[i].y-R, arr[i].x+R, arr[i].y+R);
  end;
  step:= 1;
  sum:= N;
  Timer1.Enabled:= true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var i, fr, j: integer;
begin
  for i := 0 to N-2 do
    if(mass[i]>mass[i+1]) then
    begin
      mass[i]:= mass[i]+mass[i+1];
      mass[i+1]:= mass[i]-mass[i+1];
      mass[i]:= mass[i]-mass[i+1];
    end;

  fr:= 0;
  for i := sum to sum+N-step- 1 do
  begin
    arr[i].num:= mass[fr];
    arr[i].x:= arr[0].x+step*(2*R+L);
    arr[i].y:= arr[fr].y;
    inc(fr);
    Canvas.Pen.Color:= clBlack;
    for j := (sum-N-1+step) to (sum-1) do
      if (arr[i].num=arr[j].num) then
        Canvas.MoveTo(arr[j].x, arr[j].y);
    Canvas.LineTo(arr[i].x, arr[i].y);
  end;

  for i := sum-1+step-N to sum-1-step+N do
  begin
    Canvas.Pen.Color:= arr[i].num;
    Canvas.Brush.Color:= arr[i].num;
    Canvas.Ellipse(arr[i].x-R, arr[i].y-R, arr[i].x+R, arr[i].y+R);
  end;

  sum:= sum+N-step;
  if (step=N-1) then
    Timer1.Enabled:= false
  else
    inc (step);
end;

end.
